package android_rteditor.customsortricheditor.editor;

import android.view.View;
import android.view.ViewGroup;

/**
 * 编辑器项操作的工具类
 * Created by chenxunlin01 on 2017/9/11.
 */

public class EditorDomUtils {

    private RichEditorController controller;

    public EditorDomUtils(RichEditorController controller) {
        this.controller = controller;
    }

    /**
     * 用新的视图替换旧的视图
     *
     * @param newItem
     * @param oldItem
     */
    public void replaceView(IEditorItem newItem, IEditorItem oldItem) {
        View newView = newItem.getView();
        View oldView = oldItem.getView();
        ViewGroup parent = (ViewGroup) oldView.getParent();
        int oldIndex = parent.indexOfChild(oldView);
        controller.removeLayoutTransition();
        parent.addView(newView, oldIndex);
        parent.removeView(oldView);
        controller.recoveryLayoutTransition();
    }

    /**
     * 文本视图转为checkbox视图
     *
     * @param richEditText
     * @param checkboxView
     */
    public void textToCheckbox(RichEditText richEditText, CheckboxView
            checkboxView) {
        int[] startEnd = richEditText.getSelectionStartEnd();
        checkboxView.setEditorText(richEditText.getText().toString());
        replaceView(checkboxView, richEditText);
        checkboxView.focusByStartEnd(startEnd[0], startEnd[1]);
    }

    /**
     * checkbox视图转为文本视图
     *
     * @param richEditText
     * @param checkboxView
     */
    public void checkboxToText(CheckboxView checkboxView, RichEditText
            richEditText) {
        int[] startEnd = checkboxView.getSelectionStartEnd();
        richEditText.setText(checkboxView.getEditorText());
        replaceView(richEditText, checkboxView);
        richEditText.focusByStartEnd(startEnd[0], startEnd[1]);
    }

    /**
     * 创建checkbox项
     *
     * @return
     */
    public CheckboxView createEditorCheckboxItem() {
        return (CheckboxView) EditorItemFactory.newstance().createEditorItem(controller, EditorItemFactory
                .EDITOR_ITEM_CHECKBOX);
    }

    /**
     * 创建文本项
     *
     * @return
     */
    public RichEditText createEditorTextItem() {
        return (RichEditText) EditorItemFactory.newstance().createEditorItem(controller, EditorItemFactory
                .EDITOR_ITEM_EDITTEXT);
    }

    /**
     * 创建图片项
     *
     * @return
     */
    public RichImage createEditorImageItem() {
        return (RichImage) EditorItemFactory.newstance().createEditorItem(controller, EditorItemFactory
                .EDITOR_ITEM_IMAGE);
    }

    /**
     * 创建音频项
     *
     * @return
     */
    public RichAudio createEditorAudioItem() {
        return (RichAudio) EditorItemFactory.newstance().createEditorItem(controller, EditorItemFactory
                .EDITOR_ITEM_AUDIO);
    }

    /**
     * 是否是块视图
     *
     * @return
     */
    public boolean isBlockItem(int type) {
        if (EditorItemFactory.EDITOR_ITEM_IMAGE == type || EditorItemFactory.EDITOR_ITEM_EDITTEXT == type) {
            return false;
        } else {
            return true;
        }
    }
}
